package com.ccb.jyzx.expand.tiantouonline.service.impl;

import com.ccb.jyzx.expand.tiantouonline.bean.Dict;
import com.ccb.jyzx.expand.tiantouonline.bean.bo.DictBo;
import com.ccb.jyzx.expand.tiantouonline.bean.bo.PageBo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.DictVo;
import com.ccb.jyzx.expand.tiantouonline.dao.DictDao;
import com.ccb.jyzx.expand.tiantouonline.service.DictService;
import com.ccb.jyzx.expand.tiantouonline.utils.IDUtils;
import com.ccb.jyzx.expand.tiantouonline.utils.UpdateTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: extend
 * @Author: jianghaolin
 * @Version: 1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DictServiceImpl implements DictService {

    @Autowired
    private DictDao dictDao;

    @Override
    public List<Dict> findAll(DictBo bo) {
        Integer level = bo.getLevel();
        String fatherCode = bo.getFatherCode();
        if(level == null){
            return dictDao.findAll();
        }else{
            return dictDao.findAllByLevel(level,fatherCode);
        }
    }

    @Override
    public void save(DictBo vo) throws SQLException {
        if(vo != null){
            Dict pojo = new Dict();
            UpdateTool.copyNullProperties(vo, pojo);
            String id = IDUtils.createNumId(32);
            pojo.setId(id);
            pojo.setCreateTime(System.currentTimeMillis());
            pojo.setState(1);
            dictDao.save(pojo);
        }
    }

    @Override
    public void update(DictBo pojo) throws SQLException {
        if(pojo.getId() != null){
            Dict dict = dictDao.getOne(pojo);
            if(dict != null){
                UpdateTool.copyNullProperties(dict, pojo);
                pojo.setUpdateTime(System.currentTimeMillis());
                dictDao.update(pojo);
            }
        }
    }

    @Override
    public void deleteMore(String ids) throws SQLException {
        List<String> idStr = new ArrayList<>();
        if(!StringUtils.isEmpty(ids)){
            String[] idArr = ids.split(",");
            if(idArr != null && idArr.length > 0){
                for(String id : idArr){
                    idStr.add(id);
                }
                dictDao.deleteMore(idStr);
            }
        }
    }

    @Override
    public int findTotleCount(PageBo bo) {
        Map<String,String> map = bo.getConditions();
        String fatherCode = "";
        String groupCode = "";
        String dictName = "";
        Integer level = 0;
        if(map != null){
            String levelStr = map.get("level");
            if(levelStr != null && !"".equals(levelStr)){
                level = Integer.parseInt(levelStr);
            }
            fatherCode = map.get("fatherCode");
            groupCode = map.get("groupCode");
            dictName = map.get("dictName");
        }
        return dictDao.findTotleCount(fatherCode,groupCode,dictName,level);
    }

    @Override
    public List<DictVo> findByPage(PageBo bo) {
        Integer currPage = bo.getCurrPage();
        Integer pageSize = bo.getPageSize();
        int startSearch = (currPage-1) * pageSize;
        Map<String,String> map = bo.getConditions();
        String fatherCode = "";
        String groupCode = "";
        String dictName = "";
        Integer level = 0;
        if(map != null){
            String levelStr = map.get("level");
            if(levelStr != null && !"".equals(levelStr)){
                level = Integer.parseInt(levelStr);
            }
            fatherCode = map.get("fatherCode");
            groupCode = map.get("groupCode");
            dictName = map.get("dictName");
        }
        List<Dict> dicts = dictDao.findByPage(startSearch,pageSize,fatherCode,groupCode,dictName,level);
        List<DictVo> vos = new ArrayList<>();
        if(dicts != null && dicts.size() > 0){
            for(Dict dict : dicts){
                DictVo vo = new DictVo();
                UpdateTool.copyNullProperties(dict,vo);
                vos.add(vo);
            }
        }
        return vos;
    }
}
